using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Common.Utilities;
using PDFPARSERLib;

namespace PdfLib
{
    public class PdfDocumentStringPatternFinder
    {
        public static PdfPosition SeekPattern(string pdfFilePath, string pattern)
        {
            int pageNumber = 1;
            Single x = 0;
            Single y = 0;
            StringBuilder buffer=new StringBuilder();
            PDFPARSERLib.Document pdfDoc=new Document();
            if(pdfDoc.Open(pdfFilePath,string.Empty))
            {
                PDFPARSERLib.Content content = new PDFPARSERLib.Content();
                PDFPARSERLib.Text text = null;
                int pageCount = pdfDoc.PageCount;
                SizeF size=new SizeF();
                bool foundPattern = false;
                for (int pageNum = 1; pageNum <= pageCount; pageNum++)
                {
                    pdfDoc.PageNo = pageNum;
                    PDFPARSERLib.Page page = pdfDoc.Page;
                    object[] dimensions = (object[])page.CropBox;
                    if (dimensions != null && dimensions.Length == 4)
                    {
                        float pageHeight = float.Parse(dimensions[3].ToString());
                        float pageWidth = float.Parse(dimensions[2].ToString());
                        size = new SizeF(pageWidth, pageHeight);
                    }
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    if (content == null)
                        text = null;
                    else
                        text = content.GetNextText();
                    while (text != null)
                    {
                        string nodeText = text.UnicodeString;
                        buffer.Append(" " + nodeText);
                        bool matched = StringUtil.Match(buffer.ToString(), pattern, StringMatchType.ExactMatch);
                        if (matched)
                        {
                            x = (Single)((object[])text.XPos)[0];
                            y = (Single)((object[])text.YPos)[0];
                            y = size.Height - y;
                            pageNumber = pageNum;
                            foundPattern = true;
                            break;
                        }
                        if (buffer.Length > 4000)
                        {
                            string lastSec = buffer.ToString().Substring(3500);
                            buffer = new StringBuilder();
                            buffer.Append(lastSec);
                        }
                        text = content.GetNextText();
                    }
                    if (foundPattern)
                        break;
                }

                pdfDoc.Close();
                
                
                if (!foundPattern)
                    throw new Exception("Unable to find pattern in pdf file");

                PdfPosition position = new PdfPosition();
                position.PageNumber = pageNumber;
                position.X = x;
                position.Y = y;
                return position;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static PdfPosition SeekFromPage(string pdfFilePath, int pageNum, string pattern)
        {
            StringBuilder buffer=new StringBuilder();
            PDFPARSERLib.Document pdfDoc=new Document();
            if (pdfDoc.Open(pdfFilePath, string.Empty))
            {
                int pageNumber = 1;
                Single x = 0;
                Single y = 0;
                SizeF size = new SizeF();

                int pageCount = pdfDoc.PageCount;
                PDFPARSERLib.Content content = new PDFPARSERLib.Content();
                PDFPARSERLib.Text text = null;
                pdfDoc.PageNo = pageNum;
                PDFPARSERLib.Page page = pdfDoc.Page;
                object[] dimensions = (object[]) page.CropBox;
                if (dimensions != null && dimensions.Length == 4)
                {
                    float pageHeight = float.Parse(dimensions[3].ToString());
                    float pageWidth = float.Parse(dimensions[2].ToString());
                    size = new SizeF(pageWidth, pageHeight);
                }
                content = pdfDoc.Page.Content;
                bool foundPattern = false;
                if (content != null)
                {
                    text = content.GetNextText();
                    while (text != null)
                    {
                        string nodeText = text.UnicodeString;
                        buffer.Append(" " + nodeText);
                        bool matched = StringUtil.Match(buffer.ToString(), pattern, StringMatchType.ExactMatch);
                        if (matched)
                        {
                            foundPattern = true;
                            x = (Single) ((object[]) text.XPos)[0];
                            y = (Single) ((object[]) text.YPos)[0];
                            y = size.Height - y;
                            pageNumber = pageNum;
                            break;
                        }
                        if (buffer.Length > 4000)
                        {
                            string lastSec = buffer.ToString().Substring(3500);
                            buffer = new StringBuilder();
                            buffer.Append(lastSec);
                        }

                        text = content.GetNextText();
                        if (text == null && pdfDoc.PageNo < pageCount)
                        {
                            pageNum += 1;
                            pdfDoc.PageNo = pageNum;
                            content = pdfDoc.Page.Content;
                            if (content == null)
                                text = null;
                            else
                                text = content.GetNextText();
                        }
                    }
                }
                if (!foundPattern)
                    throw new Exception("Unable to find pattern in pdf file");

                PdfPosition position = new PdfPosition();
                position.PageNumber = pageNumber;
                position.X = x;
                position.Y = y;
                return position;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }
    }
}
